using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Google.GData.Client;
using Google.Documents;
using System.IO;
using BusinessEntity;

namespace Bornander.UI.ListItems
{
    public partial class GDocsLocalControl : UserControl, IExtendedListItem
    {
        private void CreateMessage(string lsMessage)
        {
            MessageBox.Show(lsMessage, "GDocs Mobile Client", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
        }

        private event EventHandler Play;

        private FileInfo mFileInfo;

        public GDocsLocalControl()
        {
            InitializeComponent();
            

            SelectedChanged(false);
        }

        public GDocsLocalControl(string title, string artist, FileInfo lFileInfo)
        {
            InitializeComponent();

            Title = title;
            Artist = artist;
            mFileInfo = lFileInfo;

            SelectedChanged(false);
        }

        public string Title
        {
            get { return title.Text; }
            set { title.Text = value; }
        }

        public string Artist
        {
            get { return artist.Text; }
            set { artist.Text = value; }
        }

        #region IExtendedListItem Members

        public void SelectedChanged(bool isSelected)
        {
            if (isSelected)
            {
                Height = 72;
                artist.Visible = true;
            }
            else
            {
                Height = 40;
                artist.Visible = false;
            }
        }

        public void PositionChanged(int index)
        {
            if ((index & 1) == 0)
                BackColor = SystemColors.Control;
            else
                BackColor = SystemColors.ControlLight;
        }

        #endregion

        private void btnUpload_Click(object sender, EventArgs e)
        {
            try
            {
                // Do nada for now
                Session.mGDocsWrapper.Upload(mFileInfo);

                CreateMessage("Upload Completed");
            }
            catch (Exception ex)
            {
                CreateMessage(ex.Message);
            }
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start("pword.exe", "-opendoc " + mFileInfo.FullName + "");
            }
            catch (Exception ex)
            {
                CreateMessage(ex.Message);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to delete the selected file locally?", "GDoc Mobile Client", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                try
                {
                    File.Delete(mFileInfo.FullName);
                }
                catch (Exception ex)
                {
                    CreateMessage(ex.Message);
                }
            }
        }
    }
}
